/*
 * @Author: nengwu 409791297@qq.com
 * @Date: 2020-06-28 10:14:36
 * @LastEditors: mylesshie@gmail.com
 * @LastEditTime: 2020-08-01 16:51:40
 * @description:
 */

import React from 'react'
import { Image, Text, View, TouchableWithoutFeedback, ImageBackground } from 'react-native'
import Swiper from 'react-native-swiper'
import { useNavigation } from '@react-navigation/native'
import styles from './styles'

const IndexSwiper = props => {
  const navigation = useNavigation()
  const _goDetail = (item) => {
    if (item.toUrl) {
      navigation.navigate('NewsDetail', {
        id: item.toUrl
      })
    }
  }

  const _createItems = () => {

    var newList = []
    newList = props.list

    var compare = function (obj1, obj2) {
      var val1 = obj1.createdTime;
      var val2 = obj2.createdTime;
      if (val1 < val2) {
        return 1;
      } else if (val1 > val2) {
        return -1;
      } else {
        return 0;
      }
    }
    newList.sort(compare)
    newList = newList.slice(0, 5)
    return newList.map(item => {
      return (
        <TouchableWithoutFeedback key={item.id} onPress={() => { _goDetail(item) }}>
          <View style={styles.slideItem}>
            {/* <Text>test</Text> */}
            <ImageBackground
              style={styles.slideImage}
              source={{
                uri: item.imgUrl
              }}
            >
              <Text numberOfLines={1} style={styles.slideItemText}>{item.title}</Text>
            </ImageBackground>

          </View>
        </TouchableWithoutFeedback>
      )
    })
  }
  return (
    <View style={styles.wrap}>
      <Swiper
        style={styles.swiperBox}
        autoplay={true}
        loop={true}
        paginationStyle={styles.pagination}
        activeDotColor={'#D13539'}
      >
        {_createItems()}

      </Swiper>
    </View>
  )
}

export default IndexSwiper
